﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using spbtzb;
using System.Web.UI.MobileControls;

public partial class ContentList : System.Web.UI.Page
{
    //protected string strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source='E://spbtzb.mdb';";
    protected string strConnection = spbtzb.ConString.ConnString;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //this.Page.Title = "文章列表页面";
            if (Request.Params["type"] != null)
            {                
                string type = Request.Params["type"].ToString();
                ViewState["type"] = type;
                

                string sqlCmdType = "select * from ContentType Where TypeCode='" + type + "'";
                System.Data.DataTable dtType = AccessHelper.ExecuteDataTable(strConnection, System.Data.CommandType.Text, sqlCmdType);
                string strSingle = dtType.Rows[0]["IsSingle"].ToString();
                if (strSingle == "0")
                {
                    BindTopList(type);
                    this.lbCurPage.Text = "1";
                    BindList();
                    BindControl();
                    Panel1.Visible = true;
                    Panel2.Visible = false;
                    
                }

                else
                {
                    string sqlNobbCmd = String.Format("select C.* ,T.TypeName  from Contents C, ContentAndType CT,ContentType T where C.ID=CT.ContentID And CT.TypeID=T.ID " +
                "and T.TypeCode='{0}'", type);
                    System.Data.DataTable dtContents = AccessHelper.ExecuteDataTable(strConnection, System.Data.CommandType.Text, sqlNobbCmd);

                    if (dtContents == null || dtContents.Rows.Count == 0)
                    {
                        labErr.Text = "数据错误";
                        Panel3.Visible = false;
                        Panel4.Visible = true;
                    }
                    else
                    {
                        ltArticle.Text = Common.ToString(dtContents.Rows[0]["Content"].ToString());
                        ltTitle.Text = Common.ToString(dtContents.Rows[0]["Title"].ToString()); 
                        int ViewTimes = int.Parse(dtContents.Rows[0]["ViewTimes"].ToString());
                        ViewTimes++;
                        ltCreateTime.Text = Common.ToString(dtContents.Rows[0]["CreateTime"].ToString());
                        ltViewTimes.Text = ViewTimes.ToString();
                        Panel3.Visible = true;
                        Panel4.Visible = false;
                    }

                    Panel2.Visible = true;
                    Panel1.Visible = false;
                    
                }

            }
            else
            {
                Response.Redirect("Default.aspx");
            }
            
        }
    }
    protected void BindTopList(string type)
    {
        DataTable dt = AccessHelper.ExecuteDataTable(strConnection, CommandType.Text,
//            "SELECT TOP 10 A.ID, A.Title, A.CreateTime,A.ViewTimes,C.TypeCode as TypeCode FROM Contents A, ContentType B, ContentType C WHERE A.TypeCode = B.TypeCode AND  B.ParentType = C.ParentType AND C.TypeCode = '" + type + "'");
           "select C.ID,C.Title,C.CreateTime,C.ViewTimes,T.TypeCode  from Contents C,ContentAndType CT,ContentType T where C.ID=CT.ContentID And T.ID=CT.TypeID And T.TypeCode='" + type + "' Order by C.ViewTimes");
        if (dt == null) return;
        rpTopList.DataSource = dt;
        rpTopList.DataBind();
    }


    protected void BindList()
    {
        // edit by shiney
        string type = ViewState["type"].ToString();
        DataTable dt = AccessHelper.ExecuteDataTable(strConnection, CommandType.Text,
            "select C.ID,C.Title,C.CreateTime,C.ViewTimes,T.TypeCode  from Contents C,ContentAndType CT,ContentType T where C.ID=CT.ContentID And T.ID=CT.TypeID And T.TypeCode='" + type + "'");
     //                       "SELECT ID, Title, CreateTime,ViewTimes FROM Contents WHERE TypeCode = '" + type + "'");
        if (dt == null) return;
        if (dt.Rows.Count == 0)
        {
            dvEmpty.InnerHtml += "此栏目暂无信息";
            dvbtn.Visible = false;
        }

        if (dt.Rows.Count > 0) dvbtn.Visible = true;
        PagedDataSource ps = new PagedDataSource();
        ps.AllowPaging = true;
        ps.PageSize = 16;
        rpList.DataSource = ps;
        ps.CurrentPageIndex = Convert.ToInt32(this.lbCurPage.Text) - 1;
        ps.DataSource = dt.DefaultView;

        this.btnPreviousPage.Enabled = true;
        this.btnNextPage.Enabled = true;
        if (ps.CurrentPageIndex == 0)
        {
            this.btnPreviousPage.Enabled = false;
        }
        if (ps.CurrentPageIndex == ps.PageCount - 1)
        {
            this.btnNextPage.Enabled = false;
        }
        rpList.DataBind();
        
    }
    protected void BindControl()
    {
        string type = ViewState["type"].ToString();
        string parentCode = Common.GetParentTypeCode(type);
        ltType.Text = Common.GetTypeNameByCode(type);
        this.Page.Title = ltType.Text;
        if(string.IsNullOrEmpty(parentCode))
        {
            dvNav.InnerHtml += "<a href='ContentList.aspx?type=" + type + "'>" + ltType.Text.ToString() + "</a>";
        }
        else
        {
            string parentName = Common.GetTypeNameByCode(parentCode);
            dvNav.InnerHtml += "<a href='ContentType.aspx?type=" + parentCode + "'>" + parentName +
                               "</a>&nbsp;>>&nbsp;<a href='ContentList.aspx?type=" + type + "'>" + ltType.Text.ToString() + "</a>";
        }
    }

    protected string CutString(string title)
    {
        return Common.CutString(title);
    }


    protected void btnNextPage_Click(object sender, System.EventArgs e)
    {
        this.lbCurPage.Text = Convert.ToString(Convert.ToInt32(this.lbCurPage.Text) + 1);
        this.BindList();
        this.Page.Title = ltType.Text;
    }

    protected void btnPreviousPage_Click(object sender, System.EventArgs e)
    {
        this.lbCurPage.Text = Convert.ToString(Convert.ToInt32(this.lbCurPage.Text) - 1);
        this.BindList();
        this.Page.Title = ltType.Text;
    }


    public DataTable ChildParts
    {
        get
        {
            string type = Request.Params["type"].ToString();
            string parentCode = Common.GetParentTypeCode(type);
            DataTable dt = AccessHelper.ExecuteDataTable(strConnection, CommandType.Text,
                            "SELECT ID, TypeCode, TypeName FROM ContentType WHERE  ParentType = '" + parentCode + "'");
            return dt;
        }
    }

    public string ThisType
    {
        get
        {
            string type = Request.Params["type"].ToString();
            return type;
        }
    }
}
